﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace BL.DataAccess
{
    public class Serializer
    {

        public static T Open<T>(string pPath)
        {
            T instance = default(T);
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));

                using (FileStream myFileStream = new FileStream(pPath, FileMode.Open))
                {
                    instance = (T)mySerializer.Deserialize(myFileStream);
                    myFileStream.Close();
                }

            }
            catch (Exception ex)
            {
                //throw new Exception("No se pudo abrir el archivo",ex);  
            }

            return instance;

        }

        public static void Save(object instance, string pPath)
        {
            XmlSerializer mySerializer = new XmlSerializer(instance.GetType());
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter(pPath);
            mySerializer.Serialize(myWriter, instance);
            myWriter.Close();
        }

    }
}
